#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/socket.h>

#include "payload.h"

#define NOTIFY_TICKER_DELTA 5
#define TIMER_DELTA_TIMES 60

#ifndef CLICONFIG
#define COL_BRIGHT  "^c#eeeeee^"
#define COL_GRAY    "^c#555555^"
#define COL_NOTIF   "^c#6a6b9a^"
#define COL_DIVIDER "^c#111111^^r0,0,3,32^^f3^"
#endif

static char indicator[][PICTLENGTH] = { "⢇", "⢏", "⡋", "⡛", "⠝", "⠽", "⠺", "⢺", "⢱", "⣱", "⣨", "⣬", "⣔", "⣖", "⡦", "⡧", "⢇" };

static char *
pl_change_indicator ( char * in_sym )
{
	unsigned int i;
	unsigned int size = sizeof(indicator) / PICTLENGTH;

	for ( i = 0; i < size - 1; ++i )
		if ( strncmp( in_sym, indicator[i], PICTLENGTH ) == 0 )
			return indicator[i+1];

	return indicator[0];
}

struct pl_data_t *
pl_data_init ( void )
{
	struct pl_data_t * retvalue = malloc( sizeof( struct pl_data_t ) );
	if ( retvalue == NULL )
	{
		fputs( "Failed to allocate the memory", stderr );
		exit ( 1 );
	}

	retvalue->i = 0;
	retvalue->indic = "";
	retvalue->wday = "";
	sprintf( retvalue->status, "%s", "wmstatustext status started!" );
	sprintf( retvalue->notif, "%s", "wmstatustext notif started!" );

	return retvalue;
}

void
pl_data_free ( struct pl_data_t * in_data )
{
	if ( in_data == NULL )
	{
		fputs( "Trying to free null", stderr );
		return;
	}

	free( in_data );
}

void
pl_data_fill ( struct pl_data_t * in_data )
{
	if ( in_data == NULL )
	{
		fputs( "Tried to fill failed data.", stderr );
		exit( 1 );
	}

	time_t curtime = time(0);
	in_data->ttime = localtime( &curtime );
	switch ( in_data->ttime->tm_wday )
	{
		case 1:
			in_data->wday = "MON";
			break;
		case 2:
			in_data->wday = "TUE";
			break;
		case 3:
			in_data->wday = "WED";
			break;
		case 4:
			in_data->wday = "THU";
			break;
		case 5:
			in_data->wday = "FRI";
			break;
		case 6:
			in_data->wday = "SAT";
			break;
		case 0:
			in_data->wday = "SUN";
			break;
		default:
			in_data->wday = "???";
	}

	in_data->indic = pl_change_indicator(in_data->indic);

	/*                         1  2  3 4  5 6     7    8  9 10 11 1213 14 1516   17   18 */
	sprintf( in_data->status, "%s %s %s%s %s%02d<-%02d %s %s%s %s %s%s %s %s%02d:%02d:%02d ",
		        COL_NOTIF, in_data->notif, COL_GRAY, COL_DIVIDER,                                      /* 1-4   */
		        COL_BRIGHT, in_data->ttime->tm_mday, in_data->ttime->tm_mon + 1, COL_DIVIDER,          /* 5-8   */
		        COL_BRIGHT, in_data->wday, COL_DIVIDER,                                                /* 9-11  */
		        COL_BRIGHT, in_data->indic, COL_DIVIDER,                                               /* 12-14 */
		        COL_BRIGHT, in_data->ttime->tm_hour, in_data->ttime->tm_min, in_data->ttime->tm_sec ); /* 15-18 */
}

